import csv
import numpy as np

def read_data_and_preprocess_from_file(filename):
    f = open(filename, 'r')
    # f = open("dataset/s-roaming/Order-201607-201705.csv",'r')
    reader = csv.DictReader(f)
    data=[]
    for row in reader:
        i={}
        for item in row.items():
            name = str(item[0])
            value = str(item[1])
            if name == None:
                name = ""
            if value == None:
                value = ""
            i[name.strip()] = value.strip()
        data.append(i)
    return data


raw_data = read_data_and_preprocess_from_file("dataset/statistical-info.csv")


date = ['w17','w18','w19','w20','w21','w22']
data = []
labels = []
for item in raw_data:
    row = []
    for t in date:
        row.append(int(item[t]))
    labels.append(item[''])
    data.append(row)


data = np.array(data).T

w17 = data[0]
w18 = data[1]
w19 = data[2]
w20 = data[3]
w21 = data[4]
w22 = data[5]



rank = np.array([w22,w21,w20])

rank = rank.sum(axis=0)

rank_sorted_index = rank.argsort(axis=0)


for index in rank_sorted_index:
    print labels[index]




m17 = np.mean(w17)
m18 = np.mean(w18)
m19 = np.mean(w19)
m20 = np.mean(w20)
m21 = np.mean(w21)
m22 = np.mean(w22)


c17 = np.cov(w17)
c18 = np.cov(w18)
c19 = np.cov(w19)
c20 = np.cov(w20)
c21 = np.cov(w21)
c22 = np.cov(w22)
w_sum = w17 + w18 + w19 + w20 + w21 + w22
c = np.cov(w_sum)
w_m = np.array([w17,w18,w19,w20,w21,w22])
cm = np.cov(w_m)

from sklearn import preprocessing
s17 = preprocessing.scale(w17)
s18 = preprocessing.scale(w18)
s19 = preprocessing.scale(w19)
s20 = preprocessing.scale(w20)
s21 = preprocessing.scale(w21)
s22 = preprocessing.scale(w22)


W_sum = s17 + s18 + s19 + s20 + s21 + s22
C = np.cov(W_sum)
W_m = np.array([s17,s18,s19,s20,s21,s22])
CM = np.cov(W_m)

WW = w20 + w21 + w22
WW = np.append(WW,500*3)

SS = preprocessing.MinMaxScaler()
WW1 = SS.fit_transform(WW)
WW1_mean = np.mean(WW1)
N = WW1.shape[0]

for i in range(N):
    print WW1[i],",",


cov_manual = sum((WW1 - WW1_mean)*(WW1 - WW1_mean))/(N-1)
cov_lib = np.cov(WW1)

pass
